import { Service , Inject } from "typedi";

import {ThinkEntity} from '../entity/ThinkEntity';
import {BannerEntity} from '../entity/BannerEntity';
import { getRepository,getConnection,  } from "typeorm";
import { UpdateEntity } from "../entity/UpdateEntity";

@Service()
export class HomeDao{

   
    

    public async changeLikeThinkDao(think_Id:number,isLike:string): Promise<any> {
        return await   getConnection()
        .createQueryBuilder()
        .update(ThinkEntity)
        .where("id = :id", { id: think_Id })
        .set({ think_LikeType:()=>`think_LikeType  ${isLike == 'true'?'+':'-'} 1`})
        .execute();
    }
    public async changeCollectionThinkDao(think_Id:number,isCollection:string): Promise<any> {
        return await   getConnection()
        .createQueryBuilder()
        .update(ThinkEntity)
        .where("id = :id", { id: think_Id })
        .set({ think_CollectionType:()=>`think_CollectionType  ${isCollection == 'true'?'+':'-'} 1`})
        .execute();
    }
    
    public async getBanner(banner_type:number): Promise<any> {
        return await   getRepository(BannerEntity)
        .createQueryBuilder('BannerEntity')
        .where("banner_Type = :id", { id: banner_type })
        .getMany()
    }

    public async getUpdateData(id:number): Promise<any> {
        return await   getRepository(UpdateEntity)
        .createQueryBuilder('UpdateEntity')
        .where("id = :updateId", { updateId: id })
        .getOne()
    }
    public async getThinkInfo(params:{think_Category:number,think_Id:number}): Promise<any> {
        return await   getRepository(ThinkEntity)
        .createQueryBuilder('ThinkEntity')
        .where("ThinkEntity.id = :id", { id: params.think_Id })
        .leftJoinAndSelect('ThinkEntity.think_Time','TimeEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Country','CountryEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Info','ThinkInfoEntity',)
        .getOne()
    }
    public async getThinkPaint(params:{think_Category:number,think_Id:number}): Promise<any> {
        return await   getRepository(ThinkEntity)
        .createQueryBuilder('ThinkEntity')
        .where("ThinkEntity.id = :id", { id: params.think_Id })
        .leftJoinAndSelect('ThinkEntity.think_Time','TimeEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Country','CountryEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Paint','PaintInfoEntity',)
        .getOne()
    }
    public async getThinkAncient(params:{think_Category:number,think_Id:number}): Promise<any> {
        return await   getRepository(ThinkEntity)
        .createQueryBuilder('ThinkEntity')
        .where("ThinkEntity.id = :id", { id: params.think_Id })
        .leftJoinAndSelect('ThinkEntity.think_Time','TimeEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Country','CountryEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Ancient','AncientInfoEntity',)
        .getOne()
    }
    public async getHomeThinkDao(think_Page:number,think_Count:number): Promise<any> {

        return await   getRepository(ThinkEntity)
        .createQueryBuilder('ThinkEntity')
        .leftJoinAndSelect('ThinkEntity.think_Time','TimeEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Country','CountryEntity',)
        .orderBy("ThinkEntity.think_LikeType", "DESC")
        .skip(think_Page*think_Count)
        .take(think_Count)
        .getMany()
    }

    public async getOneThinkDao(think_Id:number): Promise<any> {

        return await   getRepository(ThinkEntity)
        .createQueryBuilder('ThinkEntity')
        .leftJoinAndSelect('ThinkEntity.think_Time','TimeEntity',)
        .leftJoinAndSelect('ThinkEntity.think_Country','CountryEntity',)
        .where("ThinkEntity.id = :id", { id: think_Id })
        .getOne()
    }
    public async addWatchTypeDao(think_Id:number){

        return await getConnection()
        .createQueryBuilder()
        .update(ThinkEntity)
        .where("id = :think_Id", { think_Id: think_Id })
        .set({ think_WatchType:()=>`think_WatchType  + 1`})
        .execute();
    }
}

